package org.ade.monak.anak.service.gate.monak;

import org.ade.monak.anak.Variable.Status;
import org.ade.monak.anak.Variable.TipeKoneksi;
import org.ade.monak.anak.Variable.TipePesanMonak;
import org.ade.monak.anak.entity.DataMonitoring;
import org.ade.monak.anak.entity.IPesanData;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.entity.Peringatan;
import org.ade.monak.anak.service.gate.SenderInternet;
import org.ade.monak.anak.service.gate.SenderSMS;
import org.ade.monak.anak.service.storage.DatabaseManagerAnak;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class SenderPesanData{
	
	public SenderPesanData(Context context, Handler handler, Ortu ortu){
		senderSMS		= new SenderSMS(context, null);
		senderInternet	= new SenderInternet(context, new HandlerSenderInternetMonitoring(this));
		this.handler	= handler;
		this.ortu		= ortu;
		this.context	= context;
	}
	public void sendPeringatanTerlarang(Peringatan peringatan, DataMonitoring dataMonitoring){
		peringatan.setTipe(TipePesanMonak.PERINGATAN_TERLARANG);
		pesanData = peringatan;
		kirimPesanData(pesanData, dataMonitoring);
	}

	public void sendPeringatanSeharusnya(Peringatan peringatan, DataMonitoring dataMonitoring){
		peringatan.setTipe(TipePesanMonak.PERINGATAN_SEHARUSNYA);
		pesanData = peringatan;		
		kirimPesanData(pesanData, dataMonitoring);
	}
	
	private void kirimPesanData( IPesanData pesanData, DataMonitoring dataMonitoring ){
		switch(pesanData.getTipe()){
			case TipePesanMonak.PERINGATAN_SEHARUSNYA:{
			}case TipePesanMonak.PERINGATAN_TERLARANG:{

				dataMonitoring.setAktif(false);
				DatabaseManagerAnak databaseManager = new DatabaseManagerAnak(context);
				databaseManager.setAktifMonitoring(dataMonitoring);
				break;
			}
		}
		pesan = pesanData.getJsonPesanData();
		sendInternet(ortu.getIdOrtu(), pesan);
	}
	
	public void sendInternet(String idOrtu,String message){
		senderInternet.kirimPesan(idOrtu, message);
	}
	
	public void sendSms(String noHp, String message){
		senderSMS.kirimPesan(noHp, message);
	}
	
	public void success(int tipeKoneksi){
		if(handler==null) return;
		handler.sendEmptyMessage(Status.SUCCESS);	
		
	}
	
	public void failed(int tipeKoneksi){
		if(handler==null) return;
		if(tipeKoneksi==TipeKoneksi.INTERNET){
			sendSms(ortu.getNoHp(), pesan);
		}else{
			handler.sendEmptyMessage(Status.FAILED);
		}
	}
	
	private final SenderSMS			senderSMS;
	private final SenderInternet	senderInternet;
	private IPesanData 				pesanData;
	private final Handler			handler;
	private final Ortu				ortu;
	private String pesan;
	private final Context context;
	
	private static final class HandlerSenderInternetMonitoring extends Handler{

		public HandlerSenderInternetMonitoring(SenderPesanData senderMonitoring){
			this.senderMonitoring = senderMonitoring;
		}
		
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
				case Status.SUCCESS:{
					senderMonitoring.success(TipeKoneksi.INTERNET);
					break;
				}case Status.FAILED:{
					senderMonitoring.failed(TipeKoneksi.INTERNET);
					break;
				}
			}
		}
		private SenderPesanData senderMonitoring;
		
	}

	
}
